/**
 * Created on 2018年3月12日 下午9:06:22
 */
package com.dhb.base.service.impl;

import java.util.List;

import com.dhb.base.entity.ElectricEntity;
import com.dhb.base.service.ElectricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dhb.base.dao.ElectricDao;
import com.dhb.base.util.CheckUtils;

/**
 * 学生信息实现类 . <br>
 * 
 * @author hkb <br>
 */
@Service
@Transactional(rollbackFor = Throwable.class)
@CacheConfig(cacheNames = "Electric")
public class ElectricServiceImpl implements ElectricService {

    /**
     * 学生信息dao
     */
    @Autowired
    private ElectricDao electricDao;
    // private final ElectricDao electricDao;
    //
    // /**
    // * 另一种注入方式
    // *
    // * @param electricDao
    // */
    // @Autowired
    // public ElectricServiceImpl(ElectricDao electricDao) {
    // super();
    // this.electricDao = electricDao;
    // }

    @Override
    public String addElectric(ElectricEntity Electric) {
        electricDao.save(Electric);
        return Electric.getId();
    }

    @Override
    public String updateElectric(ElectricEntity Electric) {
        String id = Electric.getId();
//        String name = Electric.getName();
//        Integer age = Electric.getAge();
        CheckUtils.notNull(id, "id不能为空");
//        CheckUtils.notEmpty(name, "姓名不能为空");
//        CheckUtils.notNull(age, "年龄不能为空");
        // TODO 其他的校验
        return electricDao.save(Electric).getId();
    }

    @Override
    public boolean deleteElectricById(String id) {
        electricDao.deleteById(id);
        return true;
    }

    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Throwable.class)
    @Cacheable(key = "caches[0].name + ':' + #id")
    @Override
    public ElectricEntity findElectricById(String id) {
        return electricDao.findById(id).get();
    }



    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Throwable.class)
  //  @Cacheable(key = "caches[0].name")
    @Override
    public List<ElectricEntity> findAllElectric() {
        return electricDao.findAll();
    }

}
